﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace WebUI
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!IsPostBack)
            {
                ListItem item = new ListItem("", "0");
                this.ddlPFav.Items.Add(item);
                bindFavorite();
                bindTreeView();
                if (Request.QueryString["node"] != null)
                {
                    this.ddlFavorite.SelectedValue = Request.QueryString["node"].ToString();
                    this.ddlPFav.SelectedValue = Request.QueryString["node"].ToString();
                    bindRep();
                }
                
            }
           
        }

        private void bindRep()
        {
            this.repURL.DataSource = BLL.URLInfo.GetURLInfoDataSet(Convert.ToInt32(Request.QueryString["node"])).Tables[0];
            this.repURL.DataBind();
        }
        //保存URL网址
        protected void btnSaveURL_Click(object sender, EventArgs e)
        {

            Model.URLInfo modurl = new Model.URLInfo();
            modurl.Title = this.txtTitle.Text;
            modurl.Remark = this.txtRemark.Text;
            modurl.Url = this.txtURL.Text;
            modurl.FID = Convert.ToInt32(this.ddlFavorite.SelectedValue);
            string imgpath = null;
            if (UploadImage(ref imgpath)) {
                modurl.Img = imgpath;
            }
            BLL.URLInfo.UpdateURLInfo(modurl);
            bindRep();
        }
        //保存网址收藏夹
        protected void btnSaveFav_Click(object sender, EventArgs e)
        {

            Model.Favorite modfav = new Model.Favorite();
            modfav.FName = this.txtName.Text;
            modfav.UID = Convert.ToInt32(Session["uid"].ToString());
            modfav.PID = Convert.ToInt32(this.ddlPFav.SelectedValue);
            BLL.Favorite.AddFavorite(modfav);
            this.ddlFavorite.Items.Clear();
            this.ddlPFav.Items.Clear();
            ListItem item = new ListItem("", "0");
            this.ddlPFav.Items.Add(item);
            bindFavorite();
        }
        //为控件加载数据
        private void bindFavorite()
        {
            IList<Model.Favorite> list = BLL.Favorite.GetTopFavorite(Convert.ToInt32(Session["uid"].ToString()));
            if (list != null)
            {
                foreach (Model.Favorite modfav in list)
                {
                    ListItem favitem = new ListItem(modfav.FName, modfav.FID.ToString());
                    this.ddlFavorite.Items.Add(favitem);
                    this.ddlPFav.Items.Add(favitem);
                    bindChildFavorite(favitem);
                }
            }
        }
        //使用递归，使数据以数型绑定
        private void bindChildFavorite(ListItem item)
        {
             IList<Model.Favorite> childlist = BLL.Favorite.GetChildFavorite(Convert.ToInt32(item.Value.ToString()));
            if (childlist != null)
            {
                foreach (Model.Favorite modfav in childlist)
                {
                    ListItem favitem = new ListItem(item.Text + "\\" + modfav.FName, modfav.FID.ToString());
                    this.ddlFavorite.Items.Add(favitem);
                    this.ddlPFav.Items.Add(favitem);
                    bindChildFavorite(favitem);
                }
            }
        }
        //上传图片
        private bool UploadImage(ref string imgpath)
        {

            if (this.fileImg.PostedFile != null && this.fileImg.PostedFile.FileName.ToString().Trim() != "")
            {
                string photoName1 = fileImg.PostedFile.FileName; //获取初始文件名
                int i = photoName1.LastIndexOf("."); //取得文件名中最后一个"."的索引
                string newext = photoName1.Substring(i); //获取文件扩展名
                if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png")
                {
                    Response.Write("文件格式不正确!");
                    Response.End();
                }
                DateTime now = DateTime.Now; //获取系统时间

                imgpath = now.Millisecond.ToString() + "_" + fileImg.PostedFile.ContentLength.ToString() + newext; //重新为文件命名,时间毫秒部分+文件大小+扩展名
               
                try
                {
                    fileImg.PostedFile.SaveAs(Server.MapPath("../photos") +"\\"+ imgpath); // 保存文件到路径,用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用""代替
                    imgpath = "photos/" + imgpath;
                    return true;
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                    return false;
                }

            }
            return false;

        }


        //为树型控件绑定数据
        private void bindTreeView()
        {

            IList<Model.Favorite> list = BLL.Favorite.GetTopFavorite(Convert.ToInt32(Session["uid"].ToString()));


            if (list != null)
            {

                foreach (Model.Favorite modfav in list)
                {

                    TreeNode node = new TreeNode(modfav.FName, modfav.FID.ToString(), "", "Index.aspx?node=" + modfav.FID.ToString(), "window.parent.main");

                    createChildNode(node);
                    this.tvFavorite.Nodes.Add(node);
                }
            }
        }
        //使用递归绑定树型数据
        private void createChildNode(TreeNode node)
        {
            IList<Model.Favorite> childlist = BLL.Favorite.GetChildFavorite(Convert.ToInt32(node.Value));
            if (childlist != null)
            {

                foreach (Model.Favorite modfav in childlist)
                {
                    TreeNode childnode = new TreeNode(modfav.FName, modfav.FID.ToString(), "", "Index.aspx?node=" + modfav.FID.ToString(), "window.parent.main");

                    createChildNode(childnode);
                    node.ChildNodes.Add(childnode);
                }
            }
        }
       


    }
}
